package top.wz.one;

import java.util.HashMap;
import java.util.Map;

public class ServiceManager {
    //1. 实现服务注册的功能
    private static final Map<String, Provider> providerMap = new HashMap<>();
    private static final String DEFAULT_PROVIDER_NAME = "def";

    public static void serviceRegister(Provider provider) {
//        providerMap.put(DEFAULT_PROVIDER_NAME, provider);
        serviceRegister(DEFAULT_PROVIDER_NAME, provider);
    }
    // 通过方法重载对serviceRegister方法进行优化
    public static void serviceRegister(String name, Provider provider) {
        providerMap.put(name, provider);
    }
    //2. 实现提供服务的功能
    public static Service getService() {
//        return providerMap.get(DEFAULT_PROVIDER_NAME).newService();
        return getService(DEFAULT_PROVIDER_NAME);
    }

    public static Service getService(String name) {
        Provider provider = providerMap.get(name);
        if (provider == null)
            throw new IllegalArgumentException("no provider registered with name: "+name);
        return provider.newService();
    }
}
